package com.common.base.app.recycler.header

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.common.base.R
import com.common.base.databinding.BaseLoadingHeaderBinding
import com.common.base.utils.AppUtils
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.simple.SimpleComponent

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/25
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
class LoadingHeader : SimpleComponent, RefreshHeader {

    constructor(
        context: Context?
    ) : super(context, null, 0)

    constructor(
        context: Context?,
        attrs: AttributeSet?
    ) : super(context, attrs, 0)

    private var dataBinding: BaseLoadingHeaderBinding =
        BaseLoadingHeaderBinding.inflate(LayoutInflater.from(context), this, true)

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        if (newState == RefreshState.Refreshing) dataBinding.stateTv.text =
            AppUtils.getString(R.string.refresh_3)
        if (newState == RefreshState.ReleaseToRefresh) dataBinding.stateTv.text =
            AppUtils.getString(R.string.refresh_2)
        if (newState == RefreshState.RefreshFinish) dataBinding.stateTv.text =
            AppUtils.getString(R.string.refresh_4)
    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
        dataBinding.ivRefreshHeader.playAnimation()
    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        dataBinding.ivRefreshHeader.cancelAnimation()
        dataBinding.ivRefreshHeader.clearAnimation()
        dataBinding.stateTv.text = AppUtils.getString(R.string.refresh_1)
        return super.onFinish(refreshLayout, success)
    }
}